package com.seio.edc.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

public class StringUtil {
	private static Logger log = Logger.getLogger(StringUtil.class);

	/**
	 * 判断字符串是否为null
	 *
	 * @param value
	 * @return true/false
	 */
	public static boolean isEmpty(String value) {
		return value == null || value.trim().length() == 0;
	}

	public static boolean isNotEmpty(String value) {
		return value != null && value.trim().length() > 0;
	}

	/**
	 * URL检查
	 * @param pInput    要检查的字符串
	 * @return boolean  返回检查结果
	 */
	public static boolean isIncludeUrl (String pInput) {
		if(pInput == null){
			return false;
		}
		String regEx = "http[s]?:\\/\\/([\\w-]+\\.)+[\\w-]+([\\w-./?%&=]*)?";
		Pattern p = Pattern.compile(regEx);
		Matcher matcher = p.matcher(pInput);
		return matcher.find();
	} 

	/**
	 * 查看字符串中是否包含手机号
	 * @param str
	 * @return
	 */
	public static boolean isIncludePhone(String str){
		String mobileRegex = "1(3|5|8|4)\\d{9}";
		Pattern regex = Pattern.compile(mobileRegex);
		Matcher matcher = regex.matcher(str);
		return matcher.find();
	}

	/**
	 * String value is not Null
	 * @param value
	 * @return Value is nulll return true else return false
	 */
	public static boolean isNotNull(String value) {
		return value != null;
	}

	/**
	 * 验证是否为小数,包括整数 ;
	 * @param str
	 * @return
	 */
	public static boolean isDecimal(String str) {
		boolean flag = false;
		Pattern pattern = Pattern.compile("\\d+(.\\d+)$");
		if (str.length() > 0) {
			Matcher matcher = pattern.matcher(str);
			flag = true;
			if (matcher.matches() == true) {
				flag = true;
				//多于两个字符时,小数最多以一个0开头;    
				if (str.length() > 1) {
					if ((str.charAt(0) == '0') && (str.charAt(1) == '0')) {
						flag = false;
					}

				}
			}
		}
		return flag;
	}

	/**
	 * 验证是否为非负整数,>= 0;
	 * @param str
	 * @return
	 */
	public static boolean isInteger(String str) {
		boolean flag = false;
		Pattern pattern = Pattern.compile("\\d*$");
		//字符串不为空;
		if (str.length() > 0) {
			Matcher matcher = pattern.matcher(str);
			if (matcher.matches() == true) {
				flag = true;
				//除去以0开头的情况;
				if (str.length() > 1) {
					if ((str.charAt(0) == '0')) {
						flag = false;
					}
				}
			}
		}
		return flag;
	}

	/**
	 * 验证是否是0-1的小数,包括0和1;
	 * @param str
	 * @return
	 */
	public static boolean isZeroToOne(String str) {
		boolean flag = false;
		//字符串不为空;
		Pattern pattern = Pattern.compile("^[0]{1}(.\\d+)?$");
		if (str.length() > 0) {
			Matcher matcher = pattern.matcher(str);
			// 1、以0开头,符合规则的情况;
			if (matcher.matches() == true) {
				flag = true;
			}
			//2、输入的是1时的情况;
			if (str.length() == 1) {
				if (str.charAt(0) == '1') {
					flag = true;
				}
			}
			//3、输入的是1.0时的情况;
			if (str.length() > 2) {
				if (str.charAt(2) == '0') {
					flag = true;
				}
			}
		}
		return flag;
	}

	/**
	 * 补全字符串
	 * @param iputString
	 * @param stringLength
	 * @return
	 */
	public static String completionString(String iputString, int stringLength){
		int iputStringLength = iputString.length();
		for (int i = stringLength - iputStringLength; i > 0; i--) {
			iputString = "0" + iputString;
		}
		return iputString;
	}

	/**
	 * 删除左边的0，如输入“01”，返回“1”
	 * @param iputString
	 * @return
	 */
	public static String delLeftZero(String iputString){
		while(iputString.indexOf("0") == 0){
			iputString = iputString.substring(1);
		}
		return iputString;
	}

	/**
	 * 过滤特殊字符
	 * @param iputString 输入字符内容
	 * @return
	 */
	public static String stringFilter(String iputString){
		// 只允许字母和数字
		// String regEx = "[^a-zA-Z0-9]";
		// 清除掉所有特殊字符
		String regEx = "[`^&|'<>/?‘”“’]";

		if(iputString == null) {
			return "";
		}

		Pattern pattern = Pattern.compile(regEx);
		Matcher matcher = pattern.matcher(iputString);
		return matcher.replaceAll("").trim();
	}

	/**
	 * 字符过滤器
	 * @param iputString 输入字符内容
	 * @param regEx 正则表达式
	 * @return
	 */
	public static String stringFilter(String iputString, String regEx){
		if(iputString == null) {
			return "";
		}

		Pattern pattern = Pattern.compile(regEx);
		Matcher matcher = pattern.matcher(iputString);
		return matcher.replaceAll("").trim();
	}

	/**
	 * 字符过滤器
	 * @param iputString 输入字符内容
	 * @param regEx 正则表达式
	 * @param replaceString  替换成的字符
	 * @return
	 */
	public static String stringFilter(String iputString, String regEx, String replaceString){
		if(iputString == null) {
			return "";
		}

		Pattern pattern = Pattern.compile(regEx);
		Matcher matcher = pattern.matcher(iputString);
		return matcher.replaceAll(replaceString).trim();
	}

	/**
	 * 字符过滤器
	 * @param iputString 输入字符内容
	 * @param regEx 正则表达式
	 * @param replaceString  替换成的字符
	 * @return
	 */
	public static String stringFilter2(String iputString, String regEx, String replaceString){
		if(iputString == null) {
			return "";
		}

		Pattern pattern = Pattern.compile(regEx);
		Matcher matcher = pattern.matcher(iputString);
		StringBuffer sbr = new StringBuffer(); 
		while (matcher.find()) { 
			matcher.appendReplacement(sbr, replaceString); 
		} 
		matcher.appendTail(sbr); 
		log.info(sbr.toString()); 
		return sbr.toString();
	}

	public static String replaceEach(String text, String[] searchList, String[] replacementList) {
		if(StringUtils.isBlank(text)){
			return "";
		}
		return StringUtils.replaceEach(text.trim(), searchList, replacementList);
	}

	/**
	 * 查找text中包含的searchList的内容，并进行替换为""
	 * @param text
	 * @param searchList
	 * @return
	 */
	public static String replaceEach(String text, String[] searchList) {
		String[] replacementList = new String[searchList.length];
		for (int i = 0; i < searchList.length; i ++) {
			replacementList[i] = "";
		}
		return replaceEach(text, searchList, replacementList);
	}

	/**
	 * 过滤文本，防止sql查询语句注入
	 * @param text
	 * @return
	 */
	public static String replaceEach(String text) {
		String[] searchList = new String[] {"\'", "\"", "--"};
		return replaceEach(text, searchList);
	}

	/**
	 * 去左空格
	 * @param str
	 * @return
	 */
	public static String leftTrim(String str) {
		if (str == null || str.equals("")) {
			return str;
		} else {
			return str.replaceAll("^[\40]+", "");
		}
	}

	/**
	 * 去右空格
	 * @param str
	 * @return
	 */
	public static String rightTrim(String str) {
		if (str == null || str.equals("")) {
			return str;
		} else {
			return str.replaceAll("[\40]+$", "");
		}
	}
	/**
	 * 验证数字用逗号分隔字符串，比如234,5,6(主要用于sql的in 查询)
	 * @author gxp
	 * 2012-12-6 下午6:00:16
	 * @param strs
	 * @return
	 */
	public static boolean  checkNumberComma(String strs){
		if(isNotEmpty(strs)){
			String p="^[0-9]+(,[0-9]+)*$";
			return Pattern.compile(p).matcher(strs).find();
		}
		return false;
	}
	/**
	 * mysql 特殊字符转移过滤
	 * @author gxp
	 * 2012-12-12 下午7:07:28
	 * @param str
	 * @return
	 */
	public static String mysqlReplace(String str){
		if(str!=null){
			str=str.replace("\\", "\\\\\\").replace("'","\\'").replace("%","\\%").replace("_","\\_%");
		}
		return str;
	}

	public static void main(String[] args) {
		System.out.println(StringUtil.isDecimal("0.1"));

		String str = "*adCVs*34_a _09_b5*[/435^*&城池()^$$&*).{}+.|.)%%*(*.中国}34{45[]12.fd'*&999下面是中文的字符￥……{}【】。，；’“‘”？<>";
		System.out.println(str);
		System.out.println(stringFilter(str));

		String regEx = "[`'<>‘”“’-]";
		System.out.println(stringFilter(str, regEx));

		String text = "   'ab'\"'cd--efg测试\"中'    ";
		String strfix = replaceEach(text);
		System.out.println(strfix);
	}
	
	/**
	 * MD5加密
	 * @author yuxm
	 * 2012-12-12 下午7:07:28
	 * @param str
	 * @return
	 */
	public static String Md5(String plainText) { 
		String retVal = "";
		try { 
			MessageDigest md = MessageDigest.getInstance("MD5"); 
			md.update(plainText.getBytes()); 
			byte b[] = md.digest(); 
			int i; 
			StringBuffer buf = new StringBuffer(""); 
			for (int offset = 0; offset < b.length; offset++) { 
			i = b[offset]; 
			if(i<0) i+= 256; 
			if(i<16) 
			buf.append("0"); 
			buf.append(Integer.toHexString(i)); 
			} 
			retVal = buf.toString();
		} catch (NoSuchAlgorithmException e) { 
			e.printStackTrace(); 
		} 
		return retVal;
    }
	
	/**
	 * 返回2个字符窜中间的字符
	 * @author yuxm
	 * 2012-12-12 下午7:07:28
	 * @param str
	 * @return
	 */
	public static String getMidString(String all, String leftStr, String rightStr){
		int beginIndex = all.indexOf(leftStr)+leftStr.length();
		int endIndex = all.indexOf(rightStr);
		if("".equals(rightStr)){
			endIndex = all.length();
		}
		return all.substring(beginIndex, endIndex);
	}
}
